package classes;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.TimerTask;

import DB.DAOManager;

//////this is the timer task class
public class TaskOfTimer extends TimerTask{
	int hour=14;
	int mins=30;
	long predefinedmilliseconds,systemmilliseconds;
	long startoftimer;
	DAOManager dao;
	Connection conn;
	ResultSet rsmetadata,rsdata;
  TaskOfTimer() throws ClassNotFoundException, SQLException{
	  /////////convert predefined timing into milliseconds
	  predefinedmilliseconds=(hour*60)+mins;
	  predefinedmilliseconds=predefinedmilliseconds*60*1000;
	  System.out.println(" Predefined time in Milliseconds is "+predefinedmilliseconds);
	  ////////
	  
	 Calendar cal=Calendar.getInstance();
	 
	 int systemhour=cal.get(cal.HOUR_OF_DAY);
	 int systemmins=cal.get(cal.MINUTE);
	 
	 /////convert system time into milliseconds
	 
	 systemmilliseconds=(systemhour)*60+systemmins;
	 systemmilliseconds=systemmilliseconds*60*1000;
	
	 /////
	 
	 
	startoftimer=Math.abs(systemmilliseconds-predefinedmilliseconds);
	
	 dao=new DAOManager();
	 conn=dao.getConnection();
 }
	public void run() {
	
				getDatabaseMetaData();
				try {
					getClientData();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Log g=new Log();
				try {
					g.writeIntoCSV(rsmetadata,rsdata);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			//	FileWriter fw=new File();
	///rs.getString("COLUMN_NAME");
 catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	
	}
 void getDatabaseMetaData(){
	 
	 try {
			DatabaseMetaData dmd=conn.getMetaData();
			rsmetadata=dmd.getColumns(null, null, "client_master", null);/////change table name to 127 fields;
		
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 
	 
 }
 
 void getClientData() throws SQLException{
	 PreparedStatement pstmt=conn.prepareStatement("select * from client_master");
	 rsdata=pstmt.executeQuery();
	
 }
	
}
